
import { App } from "../../App";
import { LoadingBundle } from "../common/loading/LoadingBundle";
import { LoadingConfig } from "../common/loading/LoadingConfig";
import { LoadingOne } from "../common/loading/LoadingOne";
import { LoadingPublic } from "../common/loading/LoadingPublic";
import { GameLogic } from "./GameLogic";
import { IMgr } from "./IMgr";


export class LoadingMgr implements IMgr {

    constructor(
        public gameLogic: GameLogic
    ) {

    }

    private _startArr: LoadingOne[] = null;
    private _allArr: LoadingOne[] = null;

    // private _arrAll: ILoading[] = null;

    private _allCompleted: boolean = null;
    get allCompleted(): boolean {
        return this._allCompleted;
    }


    init() {
        /**
         * load bundles 加载bundle 
         * 
         * load public 等bundle加载完 加载public
         * 
         * load configs 可以同时加载configs
         */

        const loadingBundle = new LoadingOne(new LoadingBundle(App.ins.resMgr, App.ins.bundleMgr));

        const loadingPublic = new LoadingOne(new LoadingPublic(App.ins.bundleMgr));
        loadingBundle.addNext(loadingPublic);

        const loadingConfig = new LoadingOne(new LoadingConfig(App.ins.bundleMgr, App.ins.configMgr));
        loadingBundle.addNext(loadingConfig);

        this._startArr = [loadingBundle, /*loadingConfig*/];

        this._allArr = [loadingBundle, loadingPublic, loadingConfig];

    }
    finit() {

    }

    startLoading(): void {

        App.ins.timerMgr.setInterval(this.update, this, 0.02);

        const arr = this._startArr;

        for (const one of arr) {
            if (!one) {
                continue;
            }
            one.loading.load();
        }

    }

    update(dt: number) {
        let allCompleted = true;
        for (const one of this._allArr) {
            const completed = one.loading.checkCompleted();
            if (!completed) {
                allCompleted = false;
            } 
        }
        if (allCompleted) {
            App.ins.timerMgr.clearInterval(this.update, this);
            this.gameLogic.onLoadingMgrCompleted();
        }
        this._allCompleted = allCompleted;
    }

    getDesc(): string {
        if (this._allCompleted) {
            return "正在进入游戏..."
        }

        return ""
    }

    getProgressDesc(loading: LoadingOne): string {
        return ""
    }

}